package com.gameloft.futbol;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;


public class Util
{
	
	private static int CLIP_STACK_SIZE = 10;
	private static int[] clipStack = new int[CLIP_STACK_SIZE * 4];
	private static int clipCount = 0;
	
	
	public static void fillPseudoAlphaRect(Graphics g, int rgb, int x, int y, int w, int h)
	{
		int min = (w<h)?(w-1):(h-1);
		int diff = Math.abs(w-h);
		
		g.setColor(rgb);
		
		for (int i = 0; i < min; i += 2)
			g.drawLine(x, y+i, x+i, y);
		
		for (int i = 1; i < diff; i += 2)
			g.drawLine(x+(w<h?0:i), y+min+(w<h?i:0), x+min+(w<h?0:i), y+(w<h?i:0));
		
		for (int i = 1; i <= min; i += 2)
			g.drawLine(x+(w>h?diff:0)+i, y+h-1, x+w-1, y+(h>w?diff:0)+i);
	}
	
	public static void fillGradientRect(Graphics g, int col0, int col1, int x, int y, int w, int h)
	{
		int total = w + h;
		int step = 0;
		
		int min = (w<h)?(w-1):(h-1);
		int diff = Math.abs(w-h);
		
		for (int i = 0; i<min; i++)
		{
			g.setColor(mergeColor(col0, col1, step++, total));
			g.drawLine(x, y+i, x+i, y);
		}
		
		for (int i = 0; i<diff; i++)
		{
			g.setColor(mergeColor(col0, col1, step++, total));
			g.drawLine(x+(w<h?0:i), y+min+(w<h?i:0), x+min+(w<h?0:i), y+(w<h?i:0));
		}
		
		for (int i = 0; i <= min; i++)
		{
			g.setColor(mergeColor(col0, col1, step++, total));
			g.drawLine(x+(w>h?diff:0)+i, y+h-1, x+w-1, y+(h>w?diff:0)+i);
		}
	}
	
	public static int mergeColor(int col0, int col1, int step, int total)
	{
		int r0 = (col0 >> 16) & 0xFF;
		int g0 = (col0 >> 8) & 0xFF;
		int b0 = col0 & 0xFF;
		
		int r1 = (col1 >> 16) & 0xFF;
		int g1 = (col1 >> 8) & 0xFF;
		int b1 = col1 & 0xFF;
		
		int r = r0 * (total-step) / total + r1 * step / total;
		int g = g0 * (total-step) / total + g1 * step / total;
		int b = b0 * (total-step) / total + b1 * step / total;
		
		return (r << 16) | (g << 8) | b;
	}
	
	public static Image stretchImage(Image img, int destW, int destH)
	{
		return stretchImage(img, 0, 0, img.getWidth(), img.getHeight(), destW, destH);
	}
	
	public static Image stretchImage(Image img, int srcX, int srcY, int srcW, int srcH, int destW, int destH)
	{
		int[] srcRGB = new int[srcW * srcH];
		img.getRGB(srcRGB, 0, srcW, srcX, srcY, srcW, srcH);
		
		int[] destRGB = new int[destW * destH];
		
		int scanlength = img.getWidth();
		int acumX = 0;
		int acumY = 0;
		int srcOffsY = (srcY * scanlength) + srcX;
		int destOffs = 0;
		
		for (int y = 0; y < destH; y++)
		{
			int srcOffsX = srcOffsY;
			for (int x = 0; x < destW; x++)
			{
				destRGB[destOffs++] = srcRGB[srcOffsX];
				acumX += srcW;
				while (acumX >= destW)
				{
					srcOffsX++;
					acumX -= destW;
				}
			}
			acumY += srcH;
			while (acumY >= destH)
			{
				srcOffsY += scanlength;
				acumY -= destH;
			}
		}
		
		return Image.createRGBImage(destRGB, destW, destH, true);
	}
	
	public static Image resizeImage(int[] srcRGB, int srcW, int srcH, int destW, int destH)
	{
		int[] destRGB = new int[destW * destH];
		
		int [] mappingX = new int[destW + 1];
		fillProportionalMapping(mappingX, srcW, destW);
		
		int [] mappingY = new int[destH + 1];
		fillProportionalMapping(mappingY, srcH, destH);
		
		int srcX = 0, srcY = 0;
		int destX = 0, destY = 0;
		int count = 0;
		
		while (true)
		{
			int srcOffs = (srcY * srcW) + srcX;
			int srcA = (srcRGB[srcOffs] & 0xFF000000) >> 24;
			
			int destOffs = (destY * destW) + destX;
			int destA = (destRGB[destOffs] & 0xFF000000) >> 24;
			int destR = (destRGB[destOffs] & 0x00FF0000) >> 16;
			int destG = (destRGB[destOffs] & 0x0000FF00) >> 8;
			int destB = (destRGB[destOffs] & 0x000000FF) >> 0;
			
			if (srcA != 0)
			{
				int srcR = (srcRGB[srcOffs] & 0x00FF0000) >> 16;
				int srcG = (srcRGB[srcOffs] & 0x0000FF00) >> 8;
				int srcB = (srcRGB[srcOffs] & 0x000000FF) >> 0;
				destA = ((destA * count) + srcA) / (count+1);
				destR = ((destR * count) + srcR) / (count+1);
				destG = ((destG * count) + srcG) / (count+1);
				destB = ((destB * count) + srcB) / (count+1);
			}
			count++;
			destRGB[destOffs] = (destA << 24) | (destR << 16) | (destG << 8) | destB;
			
			srcX++;
			if (srcX == mappingX[destX+1])
			{
				srcY++;
				if (srcY == mappingY[destY+1])
				{
					destX++;
					if (destX == destW)
					{
						destY++;
						if (destY == destH) break;
						destX = 0;
					}
					count = 0;
					srcY = mappingY[destY];
				}
				srcX = mappingX[destX];
			}
		}
		
		return Image.createRGBImage(destRGB, destW, destH, true);
	}
	
	public static void fillProportionalMapping(int[] mapping, int src, int dest)
	{
		int i = 0, j = 0, acum = 0;
		while (j < dest)
		{
			mapping[j++] = i;
			acum += src;
			while (acum >= dest)
			{
				i++;
				acum -= dest;
			}
		}
		mapping[dest] = src;
	}
	
	public static int[] getImageRGB(Image img)
	{
		return getImageRGB(img, 0, 0, img.getWidth(), img.getHeight());
	}
	
	public static int[] getImageRGB(Image img, int x, int y, int w, int h)
	{
		int[] srcRGB = new int[w * h];
		img.getRGB(srcRGB, 0, w, x, y, w, h);
		return srcRGB;
	}
	
	public static Image loadImage(String filename)
	{
		Image img = null;
		try
		{
			img = Image.createImage(filename);
		}
		catch (Exception e)
		{
		}
		return img;
	}
	
	public static void pushClip(Graphics g, int clipX, int clipY, int clipW, int clipH)
	{
		int offs = clipCount << 2;
		clipStack[offs++] = g.getClipX();
		clipStack[offs++] = g.getClipY();
		clipStack[offs++] = g.getClipWidth();
		clipStack[offs++] = g.getClipHeight();
		clipCount++;
		g.setClip(clipX, clipY, clipW, clipH);
		g.clipRect(clipX, clipY, clipW, clipH);
	}
	
	public static void popClip(Graphics g)
	{
		clipCount--;
		int offs = clipCount << 2;
		int clipX = clipStack[offs++];
		int clipY = clipStack[offs++];
		int clipW = clipStack[offs++];
		int clipH = clipStack[offs++];
		g.setClip(clipX, clipY, clipW, clipH);
		g.clipRect(clipX, clipY, clipW, clipH);
	}
	
}